import { previewImage } from './previewImage';

// 定义 previewImage 的选项类型
interface PreviewImageOptions {
  url?: string;
}

let globalConfig: PreviewImageOptions = {};
const install = (
  app: {
    version: string;
    config?: any;
    prototype?: any;
  },
  config: PreviewImageOptions = {},
): void => {
  globalConfig = config;

  // previewImage 并注入全局配置
  const wrappedPreviewImage = (options: PreviewImageOptions | string) => {
    if (typeof options === 'string') {
      options = { url: options };
    }
    // 优先使用局部配置，其次使用全局配置
    const finalOption = Object.assign({}, globalConfig, options);
    return previewImage(finalOption);
  };

  if (app.version.startsWith('2.')) {
    // Vue 2
    app.prototype.$hevueImgPreview = wrappedPreviewImage;
  } else {
    // Vue 3
    app.config.globalProperties.$hevueImgPreview = wrappedPreviewImage;
  }
};

// 自动安装（浏览器环境）
if (typeof window !== 'undefined' && (window as any).Vue) {
  install((window as any).Vue);
}

export { previewImage };
export default { install };
